/*
 * Map.cpp
 *
 *  Created on: Jan 19, 2012
 *      Author: jacobimo
 */
#include <stdio.h>
#include <string>
#include <string.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include "Map.h"

//ctor
Map::Map(string file)
{
	this->_blockHieght = 0;
	this->_blockWieght = 0;
	this->_mapColumn=0;
	this->_mapRow=0;
	this->_arr =NULL;
	this->_map= NULL;
	this->_filePath =file;
}

//copy ctor
Map::Map(const Map & map)
{
	//init the mambers
	this->_blockHieght= map._blockHieght;
	this->_blockWieght=map._blockWieght;
	this->_filePath=map._filePath;
	this->_mapColumn=map._mapColumn;
	this->_mapRow=map._mapRow;

	//copy the map
	if (map._map!=NULL) {
		this->_map =new Location*[map._mapRow];
		for (int i = 0; i < map._mapRow; ++i) {
			this->_map[i] = new Location[map._mapColumn];
			for (int j = 0; j < map._mapColumn; ++j) {
				this->_map[i][j] = map._map[i][j];
			}
		}
	}

	//copy the path
	for (int i = 0; i < map._path.getCount(); ++i) {
		this->_path[i] =map._path[i];
	}
}

//dtor
Map::~Map() {
	if (this->_map!=NULL) {
		for (int i= 0; i<this->_mapRow; ++i) {
			delete[] this->_map[i];
		}
		delete[] this->_map;
	}
}

//create the map
void Map::createMap()
{
	//load the map file
	this->loadfile();
}

//load the file
void Map::loadfile()    // added the read map function, need to set real var of project.
{

char arr[32];
int intarr[10] = {0};
char cNum[33] = " ";
char matrix[20][33] = {};
int  i=0,j=0;

	cout << "Start" << endl;
	  ifstream myfile ("map.txt");


	  if (myfile.is_open())
	  {
		  cout << "open" << endl;
		  if ( myfile.good() )

	    {
			cout << "good" << endl;
			  for (i=0; i<2; i++)
			  {
				  for (j=0; j <20 ; j++)
				  {
					if (i==0 && j<5 )  // first row
					  {
						       myfile.getline(arr,3, ',');
                                intarr[j]= atoi(arr) ;
					  }
					 else
						if (i==1)
							{myfile.clear();   // all of the map
								 myfile.getline(cNum,33);
								for (int k=0 ; k<=31 ;k++ )
								{
									matrix[j][k] = cNum[k];
								}
							}
				  }
			 }

	    }

	    myfile.close();
	  }
	   else cout << "Unable to open file";

	  _mapColumn= intarr[0];
	  _mapRow=intarr[1];
	  _blockHieght=intarr[2];
	  _blockWieght=intarr[3];



}

//get the current map
Location** Map::getMap() const
{
	return this->_map;
}

const Path& Map::getPath() const
{
	return this->_path;
}
